package com.ht.controller;


import cn.hutool.json.JSONUtil;
import com.ht.data.Result;
import com.ht.dto.MyDoctor;
import com.ht.dto.User;
import com.ht.service.IMyDoctorService;
import com.ht.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 代齐园
 * @since 2022-05-25
 */
@Component
@RestController
@RequestMapping("/myDoctor")
@Api(value = "关于用户选择的医生管理")
public class MyDoctorController {


    @Autowired
    private IMyDoctorService myDoctorService;

    //查询关注医生、备选医生、我的医生表,判断我的医生是否为私人医生,用定时器判断私人服务医生到期时间j
    @GetMapping("/getMyDoctor/{status}")
    @ApiOperation(value = "查询和用户操作相关的医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header"),
            @ApiImplicitParam(name = "status",value = "0/1/2 我的医生/关注医生/备选医生",required = true,paramType = "query")
    })
    public Result getMyDoctor(@RequestHeader("AuthToken")String token,@PathVariable("status")String status){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        List<MyDoctor> doctorList = myDoctorService.getMyDoctor(user.getId(),status);
        return Result.OK().setData(doctorList);
    }

    @PostMapping("/insertDoctor/{refinance}")
    //value为这个方法干什么用的
    @ApiOperation(value = "新增我的医生")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "用户token",required = true,paramType = "header"),
            @ApiImplicitParam(name = "doctor",value = "我的医生信息",required = true,paramType = "body"),
            @ApiImplicitParam(name = "refinance",value = "续费时间",required = true,paramType = "query")
    })
    public Result insertDoctor(@RequestHeader("AuthToken")String token,@RequestBody MyDoctor doctor,@PathVariable("refinance")Long refinance){
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        myDoctorService.insertDoctor(doctor,user.getId(),refinance);
        return Result.OK().setData("添加成功");
    }

    /**
    * 定时器查询私人医生到期时间
    * */
    @Scheduled(cron = "0 0 0 * * ?")
    public void selectExpiration(){
        myDoctorService.selectExpiration();
    }

}